import { handleSearchTime } from "@userList/search";
import { meassageMap } from "@userList/utils";
import { RuleObject } from "ant-design-vue/es/form/interface";
import { AxiosResponse } from "axios";
import { fetchList, refreshTable } from ".";
import { drawerFormState, drawerLoading, drawerVisible } from "./drawer";
import {
  drawerInputValidators,
  password_help,
  password_confirm_help,
  operateType,
} from "./store";
const defineApiMap = (string) => {
  const addBusiness = async (formState) =>
    await businessApi.create({
      ...lodash.omit(formState, ["id"]),
    });
  const updateBusiness = async (formState) =>
    await businessApi.update({
      merchant_uid: formState.id,
      ...lodash.omit(formState, ["id"]),
    });
  const apiMap = new Map([
    ["add", addBusiness],
    ["update", updateBusiness],
  ]);
  return new Map([[/^[a-zA-Z_]*$/, apiMap.get(string)]]);
};
export const handleSubmitApiTask = async () => {
  const formState = drawerFormState;
  const loading = drawerLoading;
  const visible = drawerVisible;
  const string = operateType.value;
  handleSearchTime(["start_date", "end_date"], formState, "YYYY-MM-DD");
  try {
    let [, api] = [...defineApiMap(string)].find(([key]) =>
      key.test(string)
    ) as [RegExp, (formState: any) => Promise<AxiosResponse<any, any>>];
    loading.value = true;
    const result: any = await api(formState);
    if (result.code != 200) return message.error(result.message);
    visible.value = false;
    const [, showMessage] = [...meassageMap(refreshTable, fetchList)].find(
      ([key]) => key.test(string)
    );
    showMessage();
  } catch (e: any) {
    message.error(e.message);
  } finally {
    loading.value = false;
  }
};

export let password_validate = async (rule: RuleObject, value: string) => {
  if (isEmptyType(value)) {
    drawerInputValidators[2] = true;
    return Promise.reject($t("placeholder16"));
  }
  if (isPassword(value)) {
    password_help.value = <i class="fs10">{$t("placeholder11")}</i>;
    drawerInputValidators[2] = false;
    return Promise.resolve();
  } else {
    password_help.value = (
      <i class="fs10">{$t("placeholder6") + "8-13" + $t("placeholder104")}</i>
    );
    drawerInputValidators[2] = true;
    return Promise.reject();
  }
};

export let password_new_confirm_validate = async (
  rule: RuleObject,
  value: string
) => {
  if (isEmptyType(value)) {
    password_confirm_help.value = <i class="fs10">{$t("placeholder14")}</i>;
    drawerInputValidators[3] = true;
    return Promise.reject();
  }
  if (!isPassword(value)) {
    drawerInputValidators[3] = true;
    return Promise.reject();
  }
  if (value !== drawerFormState.password) {
    password_confirm_help.value = <i class="fs10">{$t("placeholder13")}</i>;
    drawerInputValidators[3] = true;
    return Promise.reject();
  } else {
    password_confirm_help.value = <i class="fs10">{$t("placeholder12")}</i>;
    drawerInputValidators[3] = false;
    return Promise.resolve();
  }
};
